﻿﻿package com.glidecast.controllers
{
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.net.URLVariables;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	import com.glidecast.models.Template;
	
	public class TemplateController  extends EventDispatcher
	{
		private var ldr:URLLoader=new URLLoader;
		public static var COMPLETE:String='TemplateDownloadComplete';
		
		public function TemplateController()
		{	
		}
		private var _template:com.glidecast.models.Template;
		public function get template():Template
		{
			return _template;
		}
		public function set template(s:Template):void
		{
			_template=s;
		}
		public function load():void
		{	
			if (this._template == null)
				return;
			if (this._template.url == null)
				return;	
			if (this._template.url == '')
				return;				
			var urlReq:URLRequest = new URLRequest(this._template.url);
			ldr.addEventListener("complete", templateLoaded);
			ldr.load(urlReq);
		}
		public function templateLoaded(event:Event):void 
		{
			//security issue? bail.
			var loader:URLLoader = URLLoader(event.target);
			this._template.templateText = loader.data.toString();
			
			//turn the data into xml
			var templateXML:XML = XML(loader.data);
			//extract the maxitems and assign to the model
			this._template.maxitems = Number(templateXML.maxitems);
			
			//extract the cycledefinition and assign to the model
			this._template.cycledefinition = templateXML.cycledefinition;
			
			//extract the CSS style and assign to template
			this._template.styledefinition = templateXML.styledefinition;
			
			//extract the template type (html/svg) and assign to template
			this._template.type = templateXML.type;
			
			dispatchEvent(new Event(TemplateController.COMPLETE));			
			trace("Loaded Template");
		}
		/*
			The mergeTemplate function takes in the array of 
			items and will replace the placeholders within the
			template with the item content.
			
			The function will then return the (presumably HTML) text
			to be sent to the view for display on the screen.
			
			The template defines the display of 1 *entire* cycle in the segment.
		*/
		public function mergeTemplate(contentTitle:String, items:Array, contentFooter:String):String
		{
			//The items array will replace the items in the template in order.
			var tvReturnString:String="";	//"<html><body>";
			
			// replace the template value with the incoming content title
			
				/*var _cycleheaderdef:String = this._template.cycleheader
				_cycleheaderdef=str_replace(_cycleheaderdef, "<!--$contentTitle-->", contentTitle.substr(0, 50));
				*/
			
			//and add it to the resulting html.
				//	tvReturnString+=_cycleheaderdef;
				
			var _cycledef:String = this._template.cycledefinition;
			
			//replace the title in the template.
			_cycledef=str_replace(_cycledef, "<!--$contentTitle-->", contentTitle.substr(0, 50));
			
			// regardless of how many items are in the rss feed, they must always contain
			// three properties: title, description and enclosure.
			// For this reason, the counter increments by 3.
			
			for (var i:int=0;i<items.length;i+=3)
			{
			
				var _title:String = items[i];
				var _description:String = items[i+1];				
				var _enclosure:String;
				if ((items[i+2] !=null) &&
					(items[i+2] !=''))
				{
					_enclosure = items[i+2];
				}
				else
				{
					_enclosure = "";
				}
				
				var removeHTML:RegExp = new RegExp("<[^>]*>", "gi");
				_title = _title.replace(removeHTML, "");
				//_description = _description.replace(removeHTML, "");		
				//var _cycledef:String = this._template.cycledefinition
				//_cycledef=str_replace(_cycledef, "<!--$title-->", _title.substr(0, 50));
				_cycledef=_cycledef.replace("<!--$title-->", _title);
				_cycledef=_cycledef.replace("<!--$description-->", _description.substr(0, 255));
				_cycledef=_cycledef.replace("<!--$enclosure-->", _enclosure.substr(0, 2048));				
				
				//tvReturnString+=_cycledef;

				//trace("Template item: " + _title + "desc: " + _description + ", enc: " + _enclosure );
			}
			
			// Footer text from the function variable goes into the template
			
				/*
				var _cyclefooterdef:String = this._template.cyclefooter
				_cyclefooterdef=str_replace(_cyclefooterdef, "<!--$contentFooter-->", contentFooter.substr(0, 50));
				*/
				
			//replace the title in the template.
			_cycledef=str_replace(_cycledef, "<!--$contentFooter-->", contentFooter.substr(0, 50));
			
			// and also goes into the resulting html.
			
				/*tvReturnString+=_cyclefooterdef;
			
				tvReturnString+="</body></html>";
				trace("Return string: " + tvReturnString);
				*/
			tvReturnString = _cycledef;
			return tvReturnString;
		}
		private function str_replace(haystack:String, needle:String, replacement:String):String
		{
			var temp:Array = haystack.split(needle);
			return temp.join(replacement);
		}
		
	}
}
